//
//  VRRechargeViewController.swift
//  VoiceRoom
//
//  Created by Rae on 2024/3/14.
//

import UIKit

class VRRechargeViewController: VRBaseViewController {
    
    private let backButton          = c_make_ex(UIButton.self)
        .image("return").end()
    private let titleLabel          = c_make_ex(UILabel.self)
        .color(.white).font(20.font_bold)
        .alignment(.center).text("Recharge").end()
    private let coinsImage          = c_make_ex(UIImageView.self)
        .image("Recharge_coinBg").end()
    private let coinsLabel          = c_make_ex(UILabel.self)
        .color(.black).font(12.font_semiBold)
        .text("My Coins").end()
    private let coinsNumLabel          = c_make_ex(UILabel.self)
        .color(.black).font(26.font_medium)
        .text("125").end()
    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            backButton
            titleLabel
            coinsImage.subviews{
                coinsLabel
                coinsNumLabel
            }
            collectionView
        }
        
        backButton.top(.statusHeight + 2).leading(5).size(40)
        titleLabel.centerHorizontally()
        titleLabel.CenterY == backButton.CenterY
        coinsImage.leading(15).trailing(15).top(.navi_height + 20).height(80)
        coinsLabel.leading(30).top(15)
        coinsNumLabel.leading(30).top(30)
        collectionView.leading(0).trailing(0).bottom(0)
        collectionView.Top == coinsImage.Bottom + 15
        
        let itemW = (.screenWidth - 42.0)/3
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 5
        layout.minimumInteritemSpacing = 5
        layout.itemSize = CGSize(width: itemW, height: itemW)
        layout.sectionInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
        layout.scrollDirection = .vertical
        collectionView.style { v in
            v.collectionViewLayout = layout
            v.backgroundColor = .clear
            v.register(VRCoinsCollectionItem.self, forCellWithReuseIdentifier: VRCoinsCollectionItem.description())
            v.showsHorizontalScrollIndicator = false
            v.delegate = self
            v.dataSource = self
        }
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
    }
}

extension VRRechargeViewController: UICollectionViewDelegate,UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 9
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let item = collectionView.dequeueReusableCell(withReuseIdentifier: VRCoinsCollectionItem.description(), for: indexPath) as? VRCoinsCollectionItem else { return UICollectionViewCell() }
//        item.itemModel = dataSource[indexPath.row]
        return item
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//        let detailVC = LearningPicturesDetailViewController()
//        detailVC.itemModel = dataSource[indexPath.row]
//        self.navigationController?.pushViewController(detailVC, animated: true)
    }
}

class VRCoinsCollectionItem: UICollectionViewCell {
    
    private let mainView = c_make_ex(UIView.self)
        .bgColor(.white.alpha(0.2)).corner(20).end()
    private let coinLabel = UIIconButton(style: .leading(itemSpace: 10))
    private let moneyLabel = c_make_ex(UILabel.self)
        .text("$ 0.99").color(.black).font(12.font_semiBold)
        .bgColor("#FFFC00").corner(12.5).alignment(.center).end()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.subviews {
            mainView.subviews{
                coinLabel
                moneyLabel
            }
        }

        mainView.fillContainer()
        coinLabel.trailing(0).leading(0).top(23)
        moneyLabel.trailing(17).leading(17).height(25)
        moneyLabel.Top == coinLabel.Bottom + 16
        
        coinLabel.style { v in
            v.setImage("Recharge_coin".toImage, for: .normal)
            v.iconSize = CGSize(width: 22, height: 22)
            v.setTitleColor(.white, for: .normal)
            v.titleLabel?.font = 24.font_semiBold
            v.isUserInteractionEnabled = false
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
